<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\feedback\controller;

use plugin\saiadmin\basic\BaseController;
use app\feedback\logic\feedbackLogic;
use app\feedback\validate\feedbackValidate;
use support\Request;
use support\Response;

/**
 * 反馈问题控制器
 */
class feedbackController extends BaseController
{
    /**
     * 数据表主键
     */
    protected $pk = 'feedback_id';
    
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new feedbackLogic();
        $this->validate = new feedbackValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['user_name', ''],
            ['lang_code', 'zh'],
            ['user_id', ''],
            ['contact_number', ''],
            ['feedback_classify', ''],
            ['create_time', '']
        ]);
        $query = $this->logic->search($where) ->order('create_time desc');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }
        /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    public function save(Request $request) : Response
    {
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('save')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
       
        $result = $this->logic->save($data);
        if ($result) {
            $this->afterChange('save');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 修改数据
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request, $id) : Response
    {
        $id = $request->input('id', $id);
        $data = $request->post();
        $data['reply_time'] = date('Y-m-d h:i:s',time());
        $data['status'] =1;
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        $info = $this->logic->find($id);
        if (!$info) {
            return $this->fail('没有找到该数据');
        }
        $result = $this->logic->update($data, [$this->pk => $id]);
        if ($result) {
            $this->afterChange('update');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

}
